package com.yanqu.road.server.cmd.orewar.cross;

import com.yanqu.road.entity.crossorewar.CrossUserOrePit;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_ORE_WAR_SEARCH_PIT, desc = "跨服侦查的玩家信息")
public class CrossOreSearchUserPitMsgCmd extends AbstractCommand {

	private int activityId;

	List<CrossUserOrePit> pitList = new ArrayList<>();

	private long userId;

	private UserBaseInfo userBaseInfo;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		OreWarProto.CrossOrePitListMsg crossSyncMsg = OreWarProto.CrossOrePitListMsg.parseFrom(packet.getBytes());
		activityId = crossSyncMsg.getActivityId();
		pitList = OreWarPb.parseCrossOrePitList(crossSyncMsg.getOrePitListList());

		if(pitList.size() == 0){
			ret = GameErrorCode.E_ORE_WAR_CROSS_SEARCH_NOUSER;
			return;
		}
		//判断是否足够侦查道具
		if(!player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(GameConfig.MINE_WAR_SEARCH_CONSUME),
				eLogMoneyType.OreWar, eLogMoneyType.OreWarDetect)){
			ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
			return;
		}
		if(pitList.size() > 0){
			userId = pitList.get(0).getUserId();
			userBaseInfo = pitList.get(0).getUserBaseInfo();
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		OreWarProto.OreSearchUserOrePitRespMsg.Builder respMsg = OreWarProto.OreSearchUserOrePitRespMsg.newBuilder();
		if(0 == ret){
			PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(userId,userBaseInfo);
			if(null != userMsg){
				respMsg.setUserData(userMsg);
			}
			OreWarProto.OrePitListMsg.Builder listMsg = OreWarPb.parseCrossOrePitListMsg(pitList);
			respMsg.setOrePitList(listMsg);
		}
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_ORE_WAR_SEARCH_USER_DATA, respMsg);
	}
}
